Compute the similarity between two lists¶
Compute the similarity between two lists.
Sample data:
[“red”, “orange”, “green”, “blue”, “white”],
[“black”, “yellow”, “green”, “blue”]
Expected output:
Color1-Color2: [‘white’, ‘orange’, ‘red’]
Color2-Color1: [‘black’, ‘yellow’]
from collections import Counter
color1 = ["red", "orange", "green", "blue", "white"]
color2 = ["black", "yellow", "green", "blue"]
counter1 = Counter(color1)
# counter1: Counter({'red': 1, 'orange': 1, 'green': 1, 'blue': 1, 'white': 1})
counter2 = Counter(color2)
# counter2: Counter({'black': 1, 'yellow': 1, 'green': 1, 'blue': 1})
print("Color1-Color2: ", list(counter1 - counter2))
# counter1 - counter2: Counter({'red': 1, 'orange': 1, 'white': 1})
print("Color2-Color1: ", list(counter2 - counter1))
# counter2 - counter1: Counter({'black': 1, 'yellow': 1})
Output:
Color1-Color2: ['red', 'white', 'orange']
Color2-Color1: ['black', 'yellow']